<?php

namespace App\Http\Controllers\Api\v1;

use App\common;
use App\Common\Responses;
use App\Http\Controllers\Controller;
use App\Logic\InformationLogic;
use App\Logic\StoreLogic;
use App\Models\Comments;
use App\Models\Store;
use App\Models\StoreComment;
use Illuminate\Http\Request;

class StoreController extends Controller
{
    //
    /**
     * 门店数据展示
     * @yuankai
     */
    public function store_list(Request $request)
    {
            try {
                if ($request->method() == 'GET'){
                    $result = StoreLogic::list();
                    if ($result){
                        return common::success(20000,$result,'门店数据查询成功');
                    }
                }else{
                    throw new \Exception('','','请求方式不正确');
                }
            }catch (\Exception $exception){
                return common::fail('','',$exception->getMessage());
            }

    }

    /**
     * 门店数据添加
     * @yuankai
     */
    public function store_add(Request $request)
    {
            try {
                if ($request->method() == 'POST'){
                    $result=StoreLogic::add($request);
                    if ($result){
                        return common::success(20000,$result,'门店数据添加成功');
                    }
                }else{
                    throw new \Exception('','','请求方式不正确');
                }
            }catch (\Exception $exception){
                return common::fail('','',$exception->getMessage());
            }

    }

    /**
     * 门店删除
     * @yuankai
     */
    public function store_del(Request $request){
        try {
            if ($request->method() == 'GET'){
                $store_id=$request->get('id');
                $result=StoreLogic::del($store_id);
                if ($result){
                    return common::success(20000,$result,'房源数据删除成功');
                }
            }else{
                throw new \Exception('','','请求方式不正确');
            }
        }catch (\Exception $exception){
            return common::fail('',"",$exception->getMessage());
        }
    }

    /**
     * 门店数据查找
     * @yuankai
     */
    public function store_search(Request $request){
        try {
            if ($request->method() == 'GET'){
                $store_name=$request->get('name');
                $result=StoreLogic::search($store_name);
                if ($result){
                    return common::success(20000,$result,'门店数据搜索成功');
                }
            }else{
                throw new \Exception('','','请求方式不正确');
            }
        }catch (\Exception $exception){
            return common::fail('',"",$exception->getMessage());
        }
    }

    /**
     * 门店详情
     * @yuankai
     */
    public function store_details(Request $request){
        try {
            if ($request->method() == 'GET'){
                $id=$request->get('id');
                $result=StoreLogic::details($id);
                if ($result){
                    return common::success(20000,$result,'门店详情展示成功');
                }
            }else{
                throw new \Exception('','','请求方式不正确');
            }
        }catch (\Exception $exception){
            return common::fail('',"",$exception->getMessage());
        }
    }

    /**
     * 门店修改
     * @yuankai
     */
    public function store_modify(Request $request){
        try {
            if ($request->method() == 'POST'){
                $update = $request->post()['name'];
                $result = StoreLogic::modify($update);
                if ($result){
                    return common::success(20000,$result,'门店数据修改成功');
                }
            }else{
                throw new \Exception('','','请求方式不正确');
            }
        }catch (\Exception $exception){
            return common::fail('',"",$exception->getMessage());
        }
    }

    /**
     * 查看所有评论
     * @param Request $request
     * @return false|string
     * @yuankai
     */
    public function store_comments(Request $request){
        try {
            //门店id
            $id = (integer) $request->get('id');
            if (empty($id)) throw new \Exception('请问要查看谁的评论呢？');

            $stores = Store::find($id);
            $comments = $stores->comments->toArray();
            //返回数据
            $data=$this->AllComments($comments);
            return common::success($data);

        }catch (\Exception $exception){
            return common::fail([],0,$exception->getMessage());
        }
    }

    //递归
    public function AllComments( $data,$id=0,$arr=[]){
        foreach ($data as $item => $value) {
            if ($value['parent_id'] == $id){
                $value['child']=$this->AllComments($data,$value['id']);
                $arr[]=$value;
            }
        }
        return $arr;
    }

    /**
     * 评论&回复评论
     * @yuankai
     * */
    public function store_publish(Request $request){
        //获取上级评论id
        $pid = (integer) $request->post('pid');
        //获取门店id
        $store_id = (integer) $request->post('store_id');
        //获取评论内容
        $comment= (string) $request->post('comment');
        //获取用户信息
        $name = (string) $request->post('name');
        //初始化内容信息
        $param=[
            'name' => $name,
            'store_id'=>$store_id,
            'comment'=>$comment,
            'status'=>1
        ];
        //判断上级评论是否为空，为空则添加评论一级不为空则判断上级评论是否存在且是否删除
        if (!empty($pid)){
            $comment= Comments::commentStatus($pid);
            if (empty($comment)) return common::fail(['error'=>'不存在的评论信息！']);
            $param['parent_id']=$comment['id'];
            $param['status']="1";
            //回复一条评论
            Comments::topComments($param);
            return common::success(['success'=>true]);
        }
        //创建一条评论
        Comments::topComments($param);
        return common::success(['success'=>true]);
    }
}
